<?php

namespace App\Http\Controllers\Admin\Article;

use App\Library\Upload;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Mockery\CountValidator\Exception;

class ArticleController extends Controller {

    public function add(Request $request) {
        $data = $request->input('data');
        if(!isset($data['title']) && empty($data['titile'])) {
            return $this->ajax(false, '标题不能为空');
        }
        if(!isset($data['content']) && empty($data['content'])) {
            return $this->ajax(false, '内容不能为空');
        }
        if(!isset($data['category_id']) && empty($data['category_id'])) {
            return $this->ajax(false, '分类不能为空');
        }
        if(!isset($data['status']) && empty($data['status'])) {
            return $this->ajax(false, '状态不能为空');
        }
        $user          = $request->session()->get(md5('user'));
        $data['auth']  = $user->user_name;
        $data['title'] = trim($data['title']);
        $is_title      = DB::table('article')->where('title', '=', $data['title'])->count();//是否存次在文章
        if($is_title > 0) {
            return $this->ajax(false, '文章已经存在');
        }

        if(!isset($data['intro']) || empty($data['intro'])) {
            $data['intro'] = substr(strip_tags($data['content']), 0, 200);
        }
        $img_url = isset($data['img']) ? $data['img'] : '';
        unset($data['img']);
        unset($data['tag_id']);
        $articleId = DB::table('article')->insertGetId($data);
        if($articleId) {
            if(!empty($img_url)) {
                $imgId = DB::table('article_img')->insertGetId(['article_id' => $articleId, 'img_url' => $img_url]);
                if(!$imgId) {
                    return $this->ajax(false, '文章保存成功，封面保存失败');
                }
            }
            return $this->ajax(true, '添加成功');
        }
        return $this->ajax(false, '添加失败');
    }

    public function gets(Request $request) {
        $data   = $request->input();
        $data   = [
            'size' => isset($data['size']) ? intval($data['size']) : '',
            'page' => isset($data['page']) ? intval($data['page']) : '',
        ];
        $offset = (intval($data['page']) - 1) * intval($data['size']);
        $ret    = DB::table('article')
                    ->leftJoin('category', 'article.category_id', '=', 'category.id')
                    ->leftJoin('article_img', 'article.id', '=', 'article_img.article_id')
                    ->select('article.id', 'article.auth', 'article.title', 'article.intro', 'article_img.img_url', 'category.name', 'article.status', 'article.create_at', 'article.update_at')
                    ->skip($offset)
                    ->take($data['size'])
                    ->get()
                    ->toArray();
        if(isset($ret[0]->auth)) {
            return $this->ajax(true, $ret);
        }
        return $this->ajax(false, '获取失败');
    }

    public function count() {
        $count = DB::table('article')->count();
        return $this->ajax(true, $count);
    }

    public function get(Request $request) {
        $id = $request->input('id');
        if(!isset($id) || !is_numeric($id)) {
            return $this->ajax(false, '文章获取错误');
        }
        $ret = DB::table('article')
                 ->leftJoin('category', 'article.category_id', '=', 'category.id')
                 ->leftJoin('article_img', 'article.id', '=', 'article_img.article_id')
                 ->select('article.id', 'article.auth', 'article.title', 'article.content', 'article.intro', 'article_img.img_url as img', 'category.name as category_name', 'category.id as category_id', 'article.status', 'article.create_at', 'article.update_at')
                 ->where('article.id', '=', $id)
                 ->first();
        if($ret !== null) {
            return $this->ajax(true, $ret);
        }
        return $this->ajax(false, '文章获取错误');
    }

    public function edit(Request $request) {
        $data = $request->input('data');
        if(!isset($data['title']) && empty($data['titile'])) {
            return $this->ajax(false, '标题不能为空');
        }
        if(!isset($data['id']) && empty($data['id'])) {
            return $this->ajax(false, '文章id丢失');
        }
        if(!isset($data['content']) && empty($data['content'])) {
            return $this->ajax(false, '内容不能为空');
        }
        if(!isset($data['category_id']) && empty($data['category_id'])) {
            return $this->ajax(false, '分类不能为空');
        }
        if(!isset($data['status']) && empty($data['status'])) {
            return $this->ajax(false, '状态不能为空');
        }
        $user = $request->session()->get(md5('user'));

        if(!isset($data['intro']) || empty($data['intro'])) {
            $data['intro'] = substr(strip_tags($data['content']), 0, 200);
        }
        $img_url     = isset($data['img']) ? $data['img'] : '';
        $articleData = [
            'auth'        => $user->user_name,
            'title'       => trim($data['title']),
            'intro'       => $data['intro'],
            'content'     => $data['content'],
            'category_id' => $data['category_id'],
            'status'      => $data['status'],

        ];
        try {
            $articleId = DB::table('article')->where('id', $data['id'])->update($articleData);
            if(!empty($img_url)) {
                $imgId = DB::table('article_img')->where('article_id', $data['id'])->update(['img_url' => $img_url]);
            }
            return $this->ajax(true, '修改成功');
        } catch(Exception $e) {
        }

        return $this->ajax(false, '修改失败');
    }

    public function del(Request $request) {
        $data = $request->input();

        if(isset($data['id']) && !empty($data['id'])) {
            DB::beginTransaction();
            $article    = DB::table('article')->where(['id' => intval($data['id'])])->delete();
            $articleImg = DB::table('article_img')->where(['article_id' => intval($data['id'])])->delete();
            if($article) {
                DB::commit();
                return $this->ajax(true, '删除成功');
            }
            DB::rollback();
        }
        return $this->ajax(false, '删除失败');
    }
}
